package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.context.BaseContext;
import com.heima.mapper.*;
import com.heima.pojo.dto.VendingMachineDTO;
import com.heima.pojo.dto.VendingMachinePageDTO;
import com.heima.pojo.entity.Business;
import com.heima.pojo.entity.Node;
import com.heima.pojo.entity.VendingMachine;
import com.heima.pojo.entity.VmType;
import com.heima.pojo.vo.NodeBsbusinessTypeVO;
import com.heima.pojo.vo.RegionVO;
import com.heima.pojo.vo.VendingMachineDetailsVO;
import com.heima.result.PageResult;
import com.heima.service.VendingMachineService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class VendingMachineServiceImpl implements VendingMachineService {
    @Autowired
    VendingMachineMapper vendingMachineMapper;
    @Autowired
    VmTypeMapper vmTypeMapper;
    @Autowired
    NodeMapper nodeMapper;
    @Autowired
    RegionMapper regionMapper;
    @Autowired
    BusinessMapper businessMapper;


    /**
     * 设备搜索
     */
    @Override
    public PageResult<VendingMachineDetailsVO> pagevmd(VendingMachinePageDTO dto) {
        PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
        Page<VendingMachineDetailsVO> page = vendingMachineMapper.SelectPage(dto.getInnerCode());
        for (VendingMachineDetailsVO vm : page) {
            NodeBsbusinessTypeVO nodevo = nodeMapper.selectVm(vm.getNodeId());
            VmType vmType = vmTypeMapper.selectVM(vm.getVmType());
            vm.setType(vmType);
            RegionVO region = regionMapper.nodeRegion(vm.getRegionId());
            List<Node> list = nodeMapper.select(region.getId());
            region.setNodeCount(list.size());
            region.setNodeList(null);
            Business businessType = businessMapper.nodebusinessByid(vm.getBusinessId());//待修改
            Integer vmCount = vendingMachineMapper.selectNodeId(vm.getId());
            nodevo.setVmCount(vmCount);// 填充region信息
            nodevo.setRegion(region);//填充商圈信息
            nodevo.setBusinessType(businessType); // 填充vm数量
            vm.setRegion(region);
            vm.setNode(nodevo);
        }

        return new PageResult<>(dto.getPageIndex(), dto.getPageSize(), page.getPages(), page.getTotal(), page.getResult());
    }

    /**
     * 新增设备
     */
    @Override
    public boolean add(VendingMachineDTO dto) {
        VendingMachine vm = new VendingMachine();
        vm.setVmType(dto.getVmType());
        vm.setNodeId(dto.getNodeId());
        vm.setCreateUserId(dto.getCreateUserId());

        Node node = nodeMapper.selectByNid(dto.getNodeId());
        BeanUtils.copyProperties(node,vm);
//        vm.setRegionId(node.getRegionId());
//        vm.setBusinessId(node.getBusinessId());
//        vm.setOwnerId(node.getOwnerId());
//        vm.setOwnerName(node.getOwnerName());
        VmType vmType = vmTypeMapper.selectVM(dto.getVmType());
        BeanUtils.copyProperties(vmType,vm);
//        vm.setVmType(vmType.getTypeId());
        vm.setCreateTime(LocalDateTime.now());
        vm.setUpdateTime(LocalDateTime.now());
        int result = vendingMachineMapper.add(vm);
        return result > 0;
    }
}

